var errorLogger = require("pomelo-logger").getLogger("error-log");
var flopMachineCfg = require("./cfg/flopMachine.json")
var lockingMachineCfg = require("./cfg/lockingMachine.json")
//大厅管理器
module.exports = function(app){
	console.log("lobbyMnager!!!")
	var manager = {}
	var roomList = {} 				//房间列表
	var lockList = {} 				//锁定信息
	var lockMap = {}  				//锁定玩家映射表
	var roomMap = {}				//房间映射
	var userLock = {} 				//玩家操作锁
	//初始化房间列表
	manager.init = function() {
		for(var gameType in flopMachineCfg){
			roomList[gameType] = {}
			lockList[gameType] = {}
			for(var level in flopMachineCfg[gameType]){
				roomList[gameType][level] = {}
				lockList[gameType][level] = {}
				for(var i = 0;i < flopMachineCfg[gameType][level].amount;i++){
					var info = {
						state : 0
					}
					roomList[gameType][level][i] = info
					lockList[gameType][level][i] = {lockTime : 0,uid : 0}
				}
			}
		}
	}
	manager.setRemote = function(remote) {
		manager.remote = remote
	}
	//加入大厅房间
	manager.joinLobbyRoom = function(uid,sid,gameType,level,table,cb) {
		console.log("joinLobbyRoom!!!!")
		if(!roomList[gameType] || !roomList[gameType][level] || !roomList[gameType][level][table]){
			console.log("gameType",gameType,"table",table)
			console.log(roomList,roomList[gameType])
			cb(false,"桌子不存在")
			return
		}
		if(roomList[gameType][level][table].state !== 0){
			console.log(roomList[gameType][level][table])
			cb(false,"该机台有玩家")
			return
		}
		var lockUid = lockList[gameType][level][table].uid
		var lockTime = lockList[gameType][level][table].lockTime
		if(lockUid && lockUid != uid && lockTime > (new Date()).getTime()){
			cb(false,"该机器已被锁定")
			return
		}
		if(userLock[uid]){
			cb(false,"请勿重复操作")
			return
		}
		userLock[uid] = true
		var userInfo = {}
		app.rpc.db.remote.getPlayerInfoByUid(null,uid,function(playerInfo) {
			manager.remote.newLobbyRoom(playerInfo,uid,sid,gameType,level,table,flopMachineCfg[gameType][level].basicScore,function(roomId) {
				delete userLock[uid]
				if(roomId !== false){
					var userInfo = {
						uid : playerInfo.uid,
						nickname : playerInfo.nickname,
						head : playerInfo.head
					}
					roomList[gameType][level][table].state = 1
					roomList[gameType][level][table].userInfo = userInfo
					roomMap[roomId] = {
						gameType : gameType,
						table : table,
						level : level,
						userInfo : userInfo
					}
					//广播加入房间
					manager.remote.channelService.broadcast("connector","onNotify",{"type":"lobbyRoomJoin","data":roomMap[roomId]})
					cb(true)
				}else{
					console.log("roomId error : ",roomId)
					cb(false)
				}
			})
		})
	}
	//获取大厅数据
	manager.getLobbyInfo = function(gameType,level,cb) {
		if(roomList[gameType] && roomList[gameType][level]){
			var info = {
				roomList : roomList[gameType][level],
				lockList : lockList[gameType][level],
				curTime : (new Date()).getTime()
			}
			cb(true,info)
		}else{
			cb(false,"参数错误")
		}
	}
	//锁定机器
	manager.lockingMachine = function(uid,roomId,type,cb) {
		if(!roomMap[roomId] || roomMap[roomId].userInfo.uid != uid){
			console.log(roomId,roomMap[roomId])
			cb(false,"只能在机器上留机")
			return
		}
		var info = lockingMachineCfg[type]
		if(!info){
			console.log("type",type)
			cb(false,"参数错误")
			return
		}
		if(lockMap[uid] && lockMap[uid].lockTime > (new Date()).getTime()){
			cb(false,"请勿重复留机")
			return
		}
		var needItemId = info.needItemId
		var needValue = info.needValue
		var lockTime = info.lockTime
		manager.remote.dbService.getItem(uid,needItemId,function(data) {
			if(data >= needValue){
				app.rpc.db.remote.addItemReason(null,uid,needItemId,-needValue,"留机消耗:"+type,function() {
					var gameType = roomMap[roomId].gameType
					var level = roomMap[roomId].level
					var table = roomMap[roomId].table
					lockList[gameType][level][table].lockTime = lockTime + (new Date()).getTime()
					lockList[gameType][level][table].uid = uid
					lockList[gameType][level][table].nickname = roomMap[roomId].userInfo.nickname
					var notify = {
						gameType : gameType,
						table : table,
						level : level,
						uid : uid,
						nickname : lockList[gameType][level][table].nickname,
						lockTime : lockTime
					}
					lockMap[uid] = {
						lockTime : lockList[gameType][level][table].lockTime,
						gameType : gameType,
						level : level,
						table : table
					}
					//广播锁机
					manager.remote.channelService.broadcast("connector","onNotify",{"type":"lockingMachine","data":notify})
					cb(true,lockList[gameType][level][table])
				})
			}else{
				cb(false,"金币不足")
			}
		})
	}
	//大厅房间关闭
	manager.lobbyRoomOver = function(roomId,info) {
		if(roomMap[roomId]){
			console.log("lobbyRoomOver!",roomId,info)
			var gameType = roomMap[roomId].gameType
			var level = roomMap[roomId].level
			var table = roomMap[roomId].table
			roomList[gameType][level][table] = {state : 0}
			var info = {
				gameType : gameType,
				table : table,
				level : level
			}
			//广播退出房间
			manager.remote.channelService.broadcast("connector","onNotify",{"type":"lobbyRoomQuit","data":info})
		}
	}
	//获取锁机数据
	manager.getLockinfo = function(uid) {
		console.log("getLockinfo",uid,lockMap,lockMap[uid])
		if(lockMap[uid] && lockMap[uid].lockTime < (new Date()).getTime()){
			delete lockMap[uid]
		}
		return lockMap[uid]
	}
	app.set("lobbyManager",manager)
	return manager
}